let Koa = require("koa");
let Router = require("koa-router");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
// handler 是一个处理函数，我们可以传递请求和响应，由它进行最后的响应 渲染组件
const handler = app.getRequestHandler();
app.prepare().then(() => {
  const server = new Koa();
  let router = new Router();
  // 定义路由
  server.use(router.routes());
  server.use(async (ctx, next) => {
    await handler(ctx.req, ctx.res);
    ctx.response = false;
  });
  server.listen(3001, () => console.log("server started at port 3001"));
});
